home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 13 / CU Amiga Magazine's Super CD-ROM 13 (1997)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1997-08].iso / CUCD / Graphics / irit70 / bin / iritinit.irt < prev    next >
Text File  |  1997-05-01  |  12KB  |  368 lines

  1. iritState("EchoSource", false);
  2. #
  3. # Init file for IRIT solid modeller.
  4. #
  5. iritstate( "FloatFrmt", "%8.6lg" );
  6. iritstate( "DumpLevel", 9 );
  7.  
  8. demo = procedure():
  9.     include("demo.irt");
  10.  
  11. pause = procedure():t:
  12.     printf("Press return to continue:", nil()):
  13.     t = getline(string_type);
  14.  
  15. #
  16. # Some simple functions.
  17. #
  18. min = function(x, y):
  19.     if (x > y, return = y, return = x);
  20. max = function(x, y):
  21.     if (x > y, return = x, return = y);
  22. sqr = function(x):
  23.     return = x * x;
  24. normalize = function(x):len:
  25.     return = 1.0:
  26.     if (thisobj("x") == vector_type || thisobj("x") == point_type,
  27.     len = sqrt(x * x):
  28.     return = point(coord(x, 0) / len,
  29.                coord(x, 1) / len,
  30.                coord(x, 2) / len)):
  31.     if (thisobj("return") == numeric_type,
  32.     printf("NORMALIZE: Can handle only vectors or points, found %8.6lDg\n",
  33.            list(x)));
  34. midPoint = function(pt1, pt2):
  35.     return = (pt1 + pt2) * 0.5;
  36. InterpPoint = function(pt1, pt2, t):
  37.     return = (pt1 * (1 - t) + pt2 * t);
  38. edge2d = function( x1, y1, x2, y2 ):
  39.     return = ctlpt( E2, x1, y1 ) + ctlpt( E2, x2, y2 );
  40. edge3d = function( x1, y1, z1, x2, y2, z2 ):
  41.     return = ctlpt( E3, x1, y1, z1 ) + ctlpt( E3, x2, y2, z2 );
  42. dstPtPt = function ( Pt1, Pt2 ):
  43.     return = sqrt( ( Pt1 - Pt2 ) * ( Pt1 - Pt2 ) );
  44. #
  45. # Setting a fixed normal to polygonal object.
  46. #
  47. SetNormalsInPoly = function( Pl, Normal ):V:i:
  48.    return = nil():
  49.    for ( i = 0, 1, sizeof( Pl ) - 1,
  50.        V = coord( Pl, i ):
  51.        attrib( V, "normal", Normal ):
  52.        snoc( V, return ) ):
  53.    return = poly( return, false );
  54. SetNormalsInPolyObj = function( Obj, Normal ):Pl:i:
  55.    return = SetNormalsInPoly( coord( Obj, 0 ), Normal ):
  56.    for ( i = 1, 1, sizeof( Obj ) - 1,
  57.        Pl = SetNormalsInPoly( coord( Obj, i ), Normal ):
  58.        return = return ^ Pl );
  59. SetNormalsInObjList = function( ObjList, Normal ):
  60.    return = nil();
  61. SetNormalsInObjList = function( ObjList, Normal ):Obj:i:
  62.    return = nil():
  63.    for ( i = 1, 1, sizeof( ObjList ),
  64.        Obj = nth( ObjList, i ):
  65.        if ( thisobj( "Obj" ) == list_type ,
  66.         snoc( SetNormalsInObjList( Obj, Normal ), return ),
  67.         if ( thisobj( "Obj" ) == poly_type,
  68.          snoc( SetNormalsInPolyObj( Obj, Normal ), return ),
  69.          snoc( Obj, return ) ) ) );
  70.  
  71. #
  72. # Sweep of circular cross section.
  73. #
  74. SwpCircSrf = function( AxisCrv, ScaleCrv, ScaleRefine ):
  75.     return = SwpSclSrf( circle( vector( 0, 0, 0 ), 1 ),
  76.             AxisCrv,
  77.             ScaleCrv,
  78.             off,
  79.             ScaleRefine );
  80.  
  81. #
  82. # Extractions of Control Polygon/Mesh/Points from a curve or a surface.
  83. #
  84. GetCtlPoints = function( Crv, Vecs ):i:p:
  85.     return = nil():
  86.     for ( i = 1, 1, sizeof( Crv ),
  87.     if ( Vecs,
  88.          p = coerce( coord( Crv, i - 1 ), vector_type ),
  89.          p = coerce( coord( Crv, i - 1 ), point_type ) ):
  90.     snoc( p, return )
  91.     );
  92. GetCtlPolygon = function( Crv ):
  93.     return = poly( GetCtlPoints( Crv, false ), true );
  94.  
  95. GetCtlMeshPts = function( Srf, Vecs ):l:i:j:p:rsize:csize:
  96.     return = nil():
  97.     rsize = meshsize( Srf, ROW ):
  98.     csize = meshsize( Srf, COL ):
  99.     for ( i = 1, 1, csize,
  100.         for ( j = 1, 1, rsize,
  101.          if ( Vecs,
  102.               p = coerce( coord( Srf, (i - 1) * rsize + j - 1 ),
  103.                   vector_type ),
  104.               p = coerce( coord( Srf, (i - 1) * rsize + j - 1 ),
  105.                   point_type ) ):
  106.          snoc( p, return )
  107.     )
  108.     );
  109. GetCtlMesh = function( Srf ):l:i:j:p:pl:rsize:csize:first:
  110.     first = true:
  111.     rsize = meshsize( Srf, ROW ):
  112.     csize = meshsize( Srf, COL ):
  113.     for ( i = 1, 1, rsize,
  114.     pl = nil():
  115.         for ( j = 1, 1, csize,
  116.          p = coerce( coord( Srf, (i - 1) * csize + j - 1 ), vector_type ):
  117.          snoc( p, pl )
  118.     ):
  119.     if ( first == true,
  120.          return = poly( pl, true ): first = false,
  121.          return = return + poly( pl, true ) )
  122.     ):
  123.     for ( j = 1, 1, csize,
  124.     pl = nil():
  125.         for ( i = 1, 1, rsize,
  126.          p = coerce( coord( Srf, (i - 1) * csize + j - 1 ), vector_type ):
  127.          snoc( p, pl )
  128.     ):
  129.     return = return + poly( pl, true )
  130.     );
  131.  
  132. #
  133. # Extract a network of isocurves.
  134. #
  135. GetIsoCurves = function( Srf, NumU, NumV ):domain:Umin:Umax:Vmin:Vmax:i:
  136.     return = nil():
  137.     domain = pdomain( Srf ):
  138.     Umin = nth( domain, 1 ):
  139.     Umax = nth( domain, 2 ):
  140.     Vmin = nth( domain, 3 ):
  141.     Vmax = nth( domain, 4 ):
  142.     if ( NumU > 0,
  143.         for ( i = 0, 1, NumU,
  144.         snoc( csurface( Srf, COL, Umin + (Umax - Umin) * i / NumU ),
  145.               return ) ) ):
  146.     if ( NumV > 0,
  147.     for ( i = 0, 1, NumV,
  148.         snoc( csurface( Srf, ROW, Vmin + (Vmax - Vmin) * i / NumV ),
  149.               return ) ) );
  150.  
  151. GetIsoCurveTubes = function( Srf, NumU, NumV, Rad ):domain:Umin:Umax:Vmin:Vmax:i:
  152.     return = nil():
  153.     domain = pdomain( Srf ):
  154.     Umin = nth( domain, 1 ):
  155.     Umax = nth( domain, 2 ):
  156.     Vmin = nth( domain, 3 ):
  157.     Vmax = nth( domain, 4 ):
  158.     for ( i = 0, 1, NumU,
  159.     snoc( SwpCircSrf( csurface( Srf, COL,
  160.                     Umin + (Umax - Umin) * i / NumU ),
  161.               Rad, 1 ),
  162.           return ) ):
  163.     for ( i = 0, 1, NumV,
  164.     snoc( SwpCircSrf( csurface( Srf, ROW,
  165.                     Vmin + (Vmax - Vmin) * i / NumV ),
  166.               Rad, 1 ),
  167.           return ) );
  168.  
  169. #
  170. # Approximate a (assumed to be) closed planar curve as a polygon
  171. # with n vertices.
  172. #
  173. CnvrtCrvToPolygon = function( Crv, n, IsPolyline ):ptl:pt:lastPt:t:t0:t1:dt:
  174.     ptl = nil():
  175.     t0 = nth( pdomain( Crv ), 1 ):
  176.     t1 = nth( pdomain( Crv ), 2 ):
  177.     if ( n < 2, n = 2 ):
  178.     dt = (t1 - t0) / (n - 1):
  179.     if ( IsPolyline == 0, t1 = t1 - dt ):
  180.     for ( t = t0, dt, t1 + dt / 2,
  181.     pt = ceval( Crv, t ):
  182.     snoc( pt, ptl )
  183.     ):
  184.     return = poly( ptl, IsPolyline );
  185.  
  186. #
  187. # Primitives in freeform surface form.
  188. #
  189. planeSrf = function( x1, y1, x2, y2 ):
  190.     return = ruledSrf( edge2d( x1, y1, x2, y1 ),
  191.                edge2d( x1, y2, x2, y2 ) );
  192. sphereSrf = function( Radius ):s45:
  193.     s45 = sin( 45 * pi / 180 ):
  194.     return = surfRev( cbspline( 3,
  195.                 list( ctlpt( P3, 1.0,  0.0, 0.0, -1.0 ),
  196.                       ctlpt( P3, s45, -s45, 0.0, -s45 ),
  197.                       ctlpt( P3, 1.0, -1.0, 0.0,  0.0 ),
  198.                       ctlpt( P3, s45, -s45, 0.0,  s45 ),
  199.                       ctlpt( P3, 1.0,  0.0, 0.0,  1.0 ) ),
  200.                 list( 0, 0, 0, 1, 1, 2, 2, 2 ) ) ) *
  201.     scale( vector( Radius, Radius, Radius ) );
  202. torusSrf = function( MRadius, mRad ):
  203.     return = SwpCircSrf( circle( vector( 0, 0, 0 ), MRadius ), mRad, 0 );
  204. coneSrf = function( Height, Radius ):
  205.     return = surfRev( ctlpt( E3, 0.0, 0.0, 0.0 ) +
  206.               ctlpt( E3, Radius, 0.0, 0.0 ) +
  207.               ctlpt( E3, 0.0, 0.0, Height ) );
  208. cone2Srf = function( Height, Radius1, Radius2 ):
  209.     return = surfRev( ctlpt( E3, 0.0, 0.0, 0.0 ) +
  210.               ctlpt( E3, Radius1, 0.0, 0.0 ) +
  211.               ctlpt( E3, Radius2, 0.0, Height ) +
  212.               ctlpt( E3, 0.0, 0.0, Height ) );
  213. cylinSrf = function( Height, Radius ):
  214.     return = surfRev( ctlpt( E3, 0.0, 0.0, 0.0 ) +
  215.               ctlpt( E3, Radius, 0.0, 0.0 ) +
  216.               ctlpt( E3, Radius, 0.0, Height ) +
  217.               ctlpt( E3, 0.0, 0.0, Height ) );
  218. boxSrf = function( Width, Dpth, Height ):
  219.     return = list( coerce( planeSrf( 0, 0, Width, Dpth ), e3 ),
  220.            coerce( planeSrf( 0, 0, Width, Dpth ), e3 ) *
  221.                trans( vector( 0, 0, Height ) ),
  222.            coerce( planeSrf( 0, 0, Width, Height ), e3 ) *
  223.                rotx( 90 ),
  224.            coerce( planeSrf( 0, 0, Width, Height ), e3 ) *
  225.                rotx( 90 ) * trans( vector( 0, Dpth, 0 ) ),
  226.            coerce( planeSrf( 0, 0, Height, Dpth ), e3 ) *
  227.                roty( -90 ),
  228.            coerce( planeSrf( 0, 0, Height, Dpth ), e3 ) *
  229.                roty( -90 ) * trans( vector( Width, 0, 0 ) ) );
  230. flatSrf = function( UOrder, VOrder ):
  231.     return = sbezier( list( list( ctlpt( E3, -1.0, -1.0, 0.0 ),
  232.                   ctlpt( E3, -1.0,  1.0, 0.0 ) ),
  233.                 list( ctlpt( E3,  1.0, -1.0, 0.0 ),
  234.                   ctlpt( E3,  1.0,  1.0, 0.0 ) ) ) ):
  235.     return = sraise( sraise( return, row, UOrder ), col, VOrder );
  236.             
  237.  
  238. #
  239. # Transformations.
  240. #
  241. tx = function( r ):
  242.     return = trans( vector( r, 0, 0 ) );
  243. ty = function( r ):
  244.     return = trans( vector( 0, r, 0 ) );
  245. tz = function( r ):
  246.     return = trans( vector( 0, 0, r ) );
  247. sx = function( r ):
  248.     return = scale( vector( r, 1, 1 ) );
  249. sy = function( r ):
  250.     return = scale( vector( 1, r, 1 ) );
  251. sz = function( r ):
  252.     return = scale( vector( 1, 1, r ) );
  253. sc = function( s ):
  254.     return = scale( vector( s, s, s ) );
  255. rx = function( r ):
  256.     return = rotx( r );
  257. ry = function( r ):
  258.     return = roty( r );
  259. rz = function( r ):
  260.     return = rotz( r );
  261.  
  262. RotVec2Z = function( w ):u:v: # Rotation Trans. of w dir to Z axis.
  263.     if ( abs( coord( w, 0 ) ) > abs( coord( w, 1 ) ),
  264.      u = vector( 0, 1, 0 ),
  265.      u = vector( 1, 0, 0 ) ):
  266.     w = normalize( w ):
  267.     v = normalize( u ^ w ):
  268.     u = normalize( w ^ v ):
  269.     return =
  270.     homomat( list( list( coord( u, 0 ), coord( v, 0 ), coord( w, 0 ), 0 ),
  271.                list( coord( u, 1 ), coord( v, 1 ), coord( w, 1 ), 0 ),
  272.                list( coord( u, 2 ), coord( v, 2 ), coord( w, 2 ), 0 ),
  273.                list( 0, 0, 0, 1 ) ) );
  274. RotZ2Vec = function( w ): # Rotation Trans. of Z axis to W dir.
  275.     return = RotVec2Z( w )^-1;
  276.  
  277. #
  278. # Arrows.
  279. #
  280. arrow3d = function( Pt, Dir, Length, Width, HeadLength, HeadWidth ):
  281.     return = list( cylinSrf( Length - HeadLength, Width / 2 )
  282.             * RotZ2Vec( Dir )
  283.             * trans( Pt ),
  284.            coneSrf( HeadLength, HeadWidth )
  285.             * trans( vector( 0, 0, Length - HeadLength ) )
  286.             * RotZ2Vec( Dir )
  287.             * trans( Pt ) );
  288.  
  289. #
  290. # Emulation of view, interact and other useful viewing functions using VIEWOBJ
  291. # for the default display device.
  292. #
  293. beep = procedure():command_: # Make some noise.
  294.     command_ = "BEEP":
  295.     viewobj(command_);
  296. viewclear = procedure():command_: # Clear the screen.
  297.     command_ = "CLEAR":
  298.     viewobj(command_);
  299. viewdclear = procedure():command_: # Delayed clear screen.
  300.     command_ = "DCLEAR":
  301.     viewobj(command_);
  302. viewdisc = procedure():command_: # Disconnect from this display device
  303.     command_ = "DISCONNECT":
  304.     viewobj(command_);
  305. viewexit = procedure():command_: # Force Display device to exit.
  306.     command_ = "EXIT":
  307.     viewobj(command_);
  308. viewmsave = procedure(name):command_: # Save viewing matrix under name.
  309.     command_ = "MSAVE " + name:
  310.     viewobj(command_);
  311. viewremove = procedure(name):command_: # Remove an object from display.
  312.     command_ = "REMOVE " + name:
  313.     viewobj(command_);
  314. viewanim = procedure(TMin, TMax, Dt):command_: # Animate a sequence.
  315.     command_ = "ANIMATE " ^ TMin ^ " " ^ TMax ^ " " ^ Dt:
  316.     viewobj(command_);
  317. viewstate = procedure(state):command_: # Change state of display device.
  318.     command_ = "STATE " + state:
  319.     viewobj(command_);
  320. view = procedure(none,clear): # Emulation of old VIEW command.
  321.     if (clear != 0.0, viewdclear()):
  322.     viewobj(none);
  323. interact = procedure(none): # Emulation of old INTERACT command.
  324.     viewdclear():
  325.     viewobj(none):
  326.     pause();
  327.  
  328. #
  329. # Client communication helper functions.
  330. #
  331. clntclear = procedure(h):command_: # Clear the screen.
  332.     command_ = "CLEAR":
  333.     clntwrite(h, command_);
  334. clntdclear = procedure(h):command_: # Delayed clear screen.
  335.     command_ = "DCLEAR":
  336.     clntwrite(h, command_);
  337. clntdisc = procedure(h):command_: # Disconnect from this display device
  338.     command_ = "DISCONNECT":
  339.     clntwrite(h, command_);
  340. clntexit = procedure(h):command_: # Force Display device to exit.
  341.     command_ = "EXIT":
  342.     clntwrite(h, command_);
  343. clntgetobj = function(h, Name):command_: # Get an object from a client.
  344.     command_ = "GETOBJ " + Name:
  345.     clntwrite(h, command_):
  346.     return = clntread(h, 100);
  347. clntmsave = procedure(h, name):command_: # Save viewing matrix under name.
  348.     command_ = "MSAVE " + name:
  349.     clntwrite(h, command_);
  350. clntremove = procedure(h, name):command_: # Remove an object from display.
  351.     command_ = "REMOVE " + name:
  352.     clntwrite(h, command_);
  353. clntanim = procedure(h, TMin, TMax, Dt):command_: # Animate a sequence.
  354.     command_ = "ANIMATE " ^ TMin ^ " " ^ TMax ^ " " ^ Dt:
  355.     clntwrite(h, command_);
  356. clntstate = procedure(h, state):command_: # Change state of display device.
  357.     command_ = "STATE " + state:
  358.     clntwrite(h, command_);
  359. clntview = procedure(h, none, clear): # Emulation of old VIEW command.
  360.     if (clear != 0.0, clntclear(h)):
  361.     clntwrite(h, none);
  362. cntrintr = procedure(h, none): # Emulation of old INTERACT command.
  363.     clntclear(h):
  364.     clntwrite(h, none):
  365.     pause();
  366.  
  367. iritState("EchoSource", true);
  368.